/*****************************************************************************************
   
   Copyright (c) Microsoft Corporation. All rights reserved.

   Use of this code sample is subject to the terms of the Microsoft
   Permissive License, a copy of which should always be distributed with
   this file.  You can also access a copy of this license agreement at:
   http://www.microsoft.com/resources/sharedsource/licensingbasics/permissivelicense.mspx

 ****************************************************************************************/

using System;

namespace GrooveWebServicesV12Helpers.Forms2
{
	/// <summary>
	/// Represents information about a field in a FormsTool.
	/// </summary>
	public class Field : GrooveWebServicesV12HelpersBase
	{
		/// <summary>
		/// Field data types
		/// </summary>
		public enum Type
		{
			Unknown = -1,
			String = 0,
			RichText = 1,
			Date = 2,
			Attachments = 3,
			Number = 4,
			VCard = 5
		}

		private GrooveWebServicesV12Helpers.Forms2.Form m_Form;
		
		private GrooveForms2WebService.FormsField m_FormsField;

		public Field(GrooveWebServicesV12Helpers.Forms2.Form i_Form, GrooveForms2WebService.FormsField i_FormsField) :
			base(i_FormsField.Name, i_FormsField.URI, i_Form.IdentityURL, i_Form.Context)
		{
			m_Form = i_Form;
			m_FormsField = i_FormsField;
		}

		/// <summary>
		/// The Form from which this Field is obtained
		/// </summary>
		public GrooveWebServicesV12Helpers.Forms2.Form Form
		{
			get
			{
				return m_Form;
			}
		}

		/// <summary>
		/// The ID of this Field.
		/// </summary>
		public string ID
		{
			get
			{
				return m_FormsField.ID;
			}
		}

		/// <summary>
		/// The data type of this field.
		/// </summary>
		public Field.Type DataType
		{
			get
			{
				Field.Type Type;

                string DataType = m_FormsField.DataType;
				if (DataType == "String")
					Type = Field.Type.String;
				else if (DataType == "RichText")
					Type = Field.Type.RichText;
				else if (DataType == "Date")
					Type = Field.Type.Date;
				else if (DataType == "Attachments")
					Type = Field.Type.Attachments;
				else if (DataType == "Number")
					Type = Field.Type.Number;
				else if (DataType == "VCard")
					Type = Field.Type.VCard;
				else
				{
					System.Diagnostics.Debug.Assert(false, "Unknown field type: '" + DataType + "'");
					Type = Field.Type.Unknown;
				}

				return Type;
			}
		}

		/// <summary>
		/// True if the Label contains RTF formatting information; false if the label contains plain text only.
		/// </summary>
		public bool IsRTFLabel
		{
			get
			{
				return m_FormsField.IsRTFLabel;
			}
		}

		/// <summary>
		/// The field's label. This is the name of the field as seen by the user of the tool.
		/// </summary>
		public string Label
		{
			get
			{
				return m_FormsField.Label;
			}
		}

		/// <summary>
		/// Unused
		/// </summary>
		public string ValidationParameter
		{
			get
			{
				return m_FormsField.ValidationParameter;
			}
		}

		/// <summary>
		/// Unused
		/// </summary>
		public string ValidationType
		{
			get
			{
				return m_FormsField.ValidationType;
			}
		}

		#region GrooveWebServicesV12HelpersBase Implementation

		public override string BindableURL
		{
			get
			{
				if (System.String.IsNullOrEmpty(m_BindableURL))
					m_BindableURL = m_Form.BindableURL + "/Field#" + this.ID;

				return m_BindableURL;
			}
		}

		#endregion

	}
}
